package com.magnificent.atlantis.client.ui.planner.panels;

import com.magnificent.atlantis.client.event.EventDispatcher;
import com.magnificent.atlantis.client.event.EventDispatcherListener;
import com.magnificent.atlantis.client.event.EventType;

import javax.swing.*;
import java.awt.*;
import java.text.DateFormat;
import java.util.Date;

/**
 * User: Babanin
 * Date: 14.02.2009
 * Time: 0:08:01
 */
public class TimeLabel extends JLabel implements EventDispatcherListener {

    private String labelText;
    private DateFormat dateFormat;

    public TimeLabel(String labelText, DateFormat dateFormat) {
        super("");

        this.labelText = labelText;
        this.dateFormat = dateFormat;

        setFont(new Font("Tahoma", Font.BOLD, 10));

        updateLabel(new Date());

        EventDispatcher.getInstance().addListener(this);
    }

    public void newEvent(EventType eventType, Object obj) {
        if (eventType == EventType.NEW_TIME_MINUTE) {
            updateLabel((Date) obj);
        }
    }

    public void updateLabel(Date date) {
        super.setText(labelText + dateFormat.format(date));
    }

    public void setText(String labelText) {
        this.labelText = labelText;
    }
}
